<?php


namespace frontend\controllers;
/**
 * 文章控制器
 */

use common\models\CatsModel;
use common\models\PostExtendsModel;
use common\models\UserModel;
use frontend\models\PostsForm;
use yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

class PostsController extends base\BaseController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        //是否要求登录的控制器方法控制
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index', 'create','upload','ueditor','mylist'],
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['create','upload','ueditor','mylist'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    '*' => ['get', 'post'],
                ],
            ],
        ];
    }
    public function actions()
    {
        return [
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',     //这里扩展地址别写错
                'config' => [
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}",
                ]
            ],
            'ueditor'=>[
                'class' => 'common\widgets\ueditor\UeditorAction',
                'config'=>[
                    //上传图片配置
                    'imageUrlPrefix' => "", /* 图片访问路径前缀 */
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                ]
            ]
        ];
    }
    /**
     * 文章列表
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * 创建文章
     */
    public function actionCreate()
    {
        $model = new PostsForm();
        $model->setScenario(PostsForm::SCENARIO_CREATE);
        if($model->load(Yii::$app->request->post()) && $model->validate()){
            if(!$model->create()){
                Yii::$app->session->setFlash('warning', $model->_lastError);
            }else{
                 return $this->redirect(['/posts/view', 'id'=>$model->id]);
            }
        }
        $cat = CatsModel::getAllCates();
        return $this->render('create',['model'=> $model,'cat'=> $cat]);
    }
    /**
     * 文章编辑
     */
    public function actionEdit()
    {
        $id = yii::$app->request->get('id','0');
        $model = new PostsForm();
        $model->setScenario(PostsForm::SCENARIO_UPDATE);
        if($model->load(Yii::$app->request->post()) && $model->validate()){
            $id = Yii::$app->request->post()['PostsForm']['id'];
            if(!$model->edit($id)){
                Yii::$app->session->setFlash('warning', $model->_lastError);
            }else{
                return $this->redirect(['/posts/view', 'id'=>$model->id]);
            }
        }
        $cat = CatsModel::getAllCates();
        $item = $model->getItem($id);
        $model->id = $id;
        $model->title = $item['title'];
        $model->cat_id = $item['cat_id'];
        $model->label_img = $item['label_img'];
        $model->content = $item['content'];
        $model->tags = $item['tags'];
        return $this->render('edit',['model'=> $model,'cat'=> $cat]);
    }
    /**
     * 浏览文章详情
     * @param $id
     * @return string
     * @throws yii\web\NotFoundHttpException
     */
    public function actionView($id){
        $model = new PostsForm();
        $data = $model->getViewById($id);
        if(yii::$app->user->getIsGuest()){
            $data['is_edit'] = 0;
        }else{
            $data['is_edit'] = $model->isEditShow(yii::$app->user->identity->getId(),$id);
        }

        $model = new PostExtendsModel();
        $model->upCounter(['post_id'=>$id], 'browser', 1);
        return $this->render('view',['data'=>$data]);
    }

    public function actionMylist(){
        $data = [];
        $data['search_date'] = yii::$app->request->get('date','');
        $data['user_id'] = yii::$app->user->identity->getId();
        $model = new PostsForm();
        $data['date_arr'] = $model->getMylistDateArr($data['user_id']);
        $data['user_info'] = UserModel::findOne($data['user_id']);
        $data['user_count'] = (new PostsForm())->getMyPostCount($data['user_id']);
        return $this->render('mylist',['data'=>$data]);
    }
}